package com.myapplication.ui.homefragment.shoefragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.myapplication.R;
import com.myapplication.config.JUrlConfig;
import com.myapplication.http.RetrofitService;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by zitaojiang on 2016/10/6.
 */
public class Fragment_Shoe extends Fragment {

    @Bind(R.id.lv)
    ListView lv;
    private Call<ShoeBean> call_Shoe = null;
    private List<ShoeBean.RowsBean> list;
    private Handler handler = new Handler();
    private ShoesAdapter adapter;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_food, null);
        ButterKnife.bind(this, view);
        initRetrofit();
        return view;
    }

    private void initRetrofit() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(JUrlConfig.Path.SHOE_BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService service = retrofit.create(RetrofitService.class);
        call_Shoe = service.geData_Shoe();
        call_Shoe.enqueue(new Callback<ShoeBean>() {
            @Override
            public void onResponse(Call<ShoeBean> call, Response<ShoeBean> response) {
                if (response.isSuccessful()) {
                    list = response.body().getRows();
                    adapter = new ShoesAdapter(getActivity(),list);
                    lv.setAdapter(adapter);
                    /*handler.post(new Runnable() {
                        @Override
                        public void run() {
                            adapter = new ShoeAdapter(list, getActivity());
                            recyclerView.setAdapter(adapter);
                        }
                    });*/
                }
            }

            @Override
            public void onFailure(Call<ShoeBean> call, Throwable t) {
                Toast.makeText(getActivity(), "cuowu" + t.getMessage(), Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
